// Generates sql statement code for a given .proto file.

#ifndef GOOGLE_PROTOBUF_COMPILER_SQL_GENERATOR_H__
#define GOOGLE_PROTOBUF_COMPILER_SQL_GENERATOR_H__

#include <string>

#include <google/protobuf/compiler/code_generator.h>
#include <google/protobuf/stubs/common.h>

namespace google
{
namespace protobuf
{

class Descriptor;
class FieldDescriptor;

namespace io
{
class Printer;
}

namespace compiler
{
namespace sql
{

class LIBPROTOC_EXPORT SQLGenerator : public CodeGenerator
{
public:
	SQLGenerator();
	virtual ~SQLGenerator();

	// CodeGenerator methods.
	virtual bool Generate(const FileDescriptor* file, const string& parameter,
			GeneratorContext* generator_context, string* error) const;

private:
	// Very coarse-grained lock to ensure that Generate() is reentrant.
	// Guards file_, printer_ and file_descriptor_serialized_.
	mutable Mutex mutex_;
	mutable const FileDescriptor* file_;// Set in Generate().  Under mutex_.
	mutable string file_descriptor_serialized_;
	mutable io::Printer* printer_;// Set in Generate().  Under mutex_.

	GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(SQLGenerator);

	void PrintSql(string* error) const;
	void PrintCreateTableSql(const Descriptor& message_descriptor, string* error) const;
	void PrintFieldsInTable( const Descriptor& message_descriptor, int(Descriptor::*CountFn)() const, const FieldDescriptor* (Descriptor::*GetterFn)(int) const) const;
	void PrintTableField(const FieldDescriptor& field, bool is_extension) const;
};

} // namespace sql
}// namespace compiler
} // namespace protobuf

} // namespace google
#endif  // GOOGLE_PROTOBUF_COMPILER_SQL_GENERATOR_H__

